package com.czy.moudle.service.impl;


import com.czy.moudle.domain.Result;
import com.czy.moudle.service.ContrastService;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by czyja on 2016/7/25.
 */
public class ContrastServiceImpl implements ContrastService {
    private List<String> exClude = new ArrayList<>();

    @Override
    public List<Result> contrast(Map<String, List<String>> pdm1, Map<String, List<String>> pdm2) {
        exClude.add("D_CREATE_DATE");
        exClude.add("D_MODIFY_DATE");

        List<Result> content = new ArrayList<>();

        /**
         * 两个PMD中都存在的表
         */
        Set<String> sameTableName = new HashSet<>();

        /***只存在于PDM2中的表*/
        for (String tableName : pdm2.keySet()) {
            if(! exClude.contains(tableName)){
                List<String> pdm1ColumnNames = pdm1.get(tableName);
                List<String> pdm2ColumnNames = pdm2.get(tableName);

                if (CollectionUtils.isNotEmpty(pdm1ColumnNames)) {
                    Result result = new Result();
                    result.setName(tableName);
                    result.setTable2(pdm2ColumnNames);
                    content.add(result);
                } else {
                    sameTableName.add(tableName);
                }
            }
        }

        /***只存在于PDM1中的表*/
        for (String tableName : pdm1.keySet()) {
            if(! exClude.contains(tableName)) {
                List<String> pdm2ColumnNames = pdm2.get(tableName);
                List<String> pdm1ColumnNames = pdm1.get(tableName);

                if (CollectionUtils.isNotEmpty(pdm2ColumnNames)) {
                    //只存在于PDM1中的表
                    Result result = new Result();
                    result.setName(tableName);
                    result.setTable1(pdm1ColumnNames);
                    content.add(result);
                } else {
                    sameTableName.add(tableName);
                }
            }
        }

        /***两个PMD都存在的表*/
        for (String tableName : sameTableName) {
            if (!exClude.contains(tableName)) {
                List<String> pdm2ColumnNames = pdm2.get(tableName);
                List<String> pdm1ColumnNames = pdm1.get(tableName);

                //tableName表中只存在于pdm1中的字段
                List<String> table1 = new ArrayList<>();
                for (String pdm1ColumnName : pdm1ColumnNames) {
                    if (!pdm2ColumnNames.contains(pdm1ColumnName)) {
                        table1.add(pdm1ColumnName);
                    }
                }

                //tableName表中只存在于pdm2中的字段
                List<String> table2 = new ArrayList<>();
                for (String pdm2ColumnName : pdm2ColumnNames) {
                    if (!pdm1ColumnNames.contains(pdm2ColumnName)) {
                        table2.add(pdm2ColumnName);
                    }
                }

                if (CollectionUtils.isNotEmpty(table1) || CollectionUtils.isNotEmpty(table2)) {
                    Result result = new Result();
                    result.setName(tableName);
                    result.setTable1(table1);
                    result.setTable2(table2);
                    content.add(result);
                }
            }
        }

        return content;
    }
}
